using CLELCore.Assembly.CLELInstructions;

namespace CLELCore.Assembly.ILGenerator
{
	public static class SingleILGenerator
	{
		public static CLELInstruction GenerateLoadLocal(uint local_num)
		{
			CLELInstruction ret = null;
			if(local_num == 0)
				ret = new Ldloc0();
			else if(local_num == 1)
				ret = new Ldloc1();
			else if(local_num == 2)
				ret = new Ldloc2();
			else if(local_num == 3)
				ret = new Ldloc3();
			else if(local_num > 3 && local_num < 256)
				ret = new Ldlocs((byte)local_num);
			else if(local_num >= 256 && local_num < 65536)
				ret = new Ldloc((ushort)local_num);
			return ret;
		}
		
		public static CLELInstruction GenerateLoadLocalAddress(uint local_num)
		{
			CLELInstruction ret = null;
			if(local_num < 256)
				ret = new Ldlocas((byte)local_num);
			else if(local_num >= 256 && local_num < 65536)
				ret = new Ldloca((ushort)local_num);
			return ret;
		}
		
		public static CLELInstruction GenerateStoreLocal(uint local_num)
		{
			CLELInstruction ret = null;
			if(local_num == 0)
				ret = new Stloc0();
			else if(local_num == 1)
				ret = new Stloc1();
			else if(local_num == 2)
				ret = new Stloc2();
			else if(local_num == 3)
				ret = new Stloc3();
			else if(local_num > 3 && local_num < 256)
				ret = new Stlocs((byte)local_num);
			else if(local_num >= 256 && local_num < 65536)
				ret = new Stloc((ushort)local_num);
			return ret;
		}

		public static CLELInstruction GenerateUnconditionalBranch(int offset)
		{
			CLELInstruction ret = null;
			if(offset >= -128 && offset < 128)
				ret = new Brs((sbyte)offset);
			else
				ret = new Br(offset);
			return ret;
		}
	}
}
